<template>
  <div class="home-container">
    <el-card class="welcome-card">
      <template #header>
        <div class="card-header">
          <h3>欢迎使用企业员工管理系统</h3>
        </div>
      </template>
      <div v-if="userStore.userInfo" class="welcome-content">
        <h2>你好，{{ userStore.userInfo.nickname || userStore.userInfo.username }}</h2>
        <p>当前角色: {{ userRoles }}</p>
        <p>部门: {{ userStore.userInfo.departmentName || '未分配' }}</p>
        <p>上次登录时间: {{ new Date().toLocaleString() }}</p>
      </div>
    </el-card>

    <el-row :gutter="20" class="info-cards">
      <el-col :xs="24" :sm="12" :md="8" :lg="6">
        <el-card class="box-card" @click="navigateTo('/profile')">
          <div class="info-item">
            <el-icon class="info-icon"><User /></el-icon>
            <div class="info-content">
              <div class="info-title">个人信息</div>
              <div class="info-value">查看和编辑个人资料</div>
            </div>
          </div>
        </el-card>
      </el-col>

      <el-col :xs="24" :sm="12" :md="8" :lg="6" v-if="userStore.hasAnyRole(['ADMIN', 'MANAGER'])">
        <el-card class="box-card" @click="navigateTo('/users')">
          <div class="info-item">
            <el-icon class="info-icon"><UserFilled /></el-icon>
            <div class="info-content">
              <div class="info-title">用户管理</div>
              <div class="info-value">管理员工和部门信息</div>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import { User, UserFilled } from '@element-plus/icons-vue'
import { useUserStore } from '@/stores/user'
import { useRouter } from 'vue-router'

const userStore = useUserStore()
const router = useRouter()

// 导航到指定路径
const navigateTo = (path: string) => {
  router.push(path)
}

// 获取用户角色，格式化显示
const userRoles = computed(() => {
  if (!userStore.userInfo || !userStore.userInfo.roles) return '无'
  
  const roleMap: Record<string, string> = {
    'ADMIN': '系统管理员',
    'MANAGER': '部门经理',
    'EMPLOYEE': '普通员工'
  }
  
  return userStore.userInfo.roles.map(role => roleMap[role] || role).join(', ')
})
</script>

<style scoped>
.home-container {
  padding: 20px;
}

.welcome-card {
  margin-bottom: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.welcome-content {
  padding: 10px;
}

.info-cards {
  margin-top: 20px;
}

.box-card {
  height: 120px;
  margin-bottom: 20px;
  cursor: pointer;
  transition: all 0.3s;
}

.box-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.info-item {
  display: flex;
  align-items: center;
  height: 100%;
}

.info-icon {
  font-size: 40px;
  margin-right: 15px;
  color: #409EFF;
}

.info-content {
  display: flex;
  flex-direction: column;
}

.info-title {
  font-size: 18px;
  font-weight: bold;
  margin-bottom: 8px;
}

.info-value {
  font-size: 14px;
  color: #666;
}
</style> 